<?php
class TinthongbaosController extends AppController {

	var $name = 'Tinthongbaos';

	function index() {
		$this->Tinthongbao->recursive = 0;
		$this->set('tinthongbaos', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid tinthongbao', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('tinthongbao', $this->Tinthongbao->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Tinthongbao->create();
			if ($this->Tinthongbao->save($this->data)) {
				$this->Session->setFlash(__('The tinthongbao has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The tinthongbao could not be saved. Please, try again.', true));
			}
		}
		$z1users = $this->Tinthongbao->Z1user->find('list');
		$khoas = $this->Tinthongbao->Khoa->find('list');
		$this->set(compact('z1users', 'khoas'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid tinthongbao', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Tinthongbao->save($this->data)) {
				$this->Session->setFlash(__('The tinthongbao has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The tinthongbao could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Tinthongbao->read(null, $id);
		}
		$z1users = $this->Tinthongbao->Z1user->find('list');
		$khoas = $this->Tinthongbao->Khoa->find('list');
		$this->set(compact('z1users', 'khoas'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for tinthongbao', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Tinthongbao->delete($id)) {
			$this->Session->setFlash(__('Tinthongbao deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Tinthongbao was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}

	function listthongbao()
	{
		$this->Tinthongbao->recursive = 0;
		$this->set('tinthongbaos', $this->paginate(array('z1user_id'=>$this->Session->read('userid'))));
	}
	
	function xemthongbao($id = null)
	{
		if (!$id) {
			$this->Session->setFlash(__('Invalid tinthongbao', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('tinthongbao', $this->Tinthongbao->read(null, $id));
	}

	function xoathongbao($id = null)
	{
		if (!$id) {
			$this->Session->setFlash(__('Thông báo không tồn tại', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Tinthongbao->delete($id)) {
			$this->Session->setFlash(__('Đã xóa thông báo', true),'success');
			$this->redirect(array('action'=>'listthongbao'));
		}
		$this->Session->setFlash(__('Lỗi khi xóa', true),'error');
		$this->redirect(array('action' => 'listthongbao'));
	}

	
}
